package com.example.mountainwalker.mapper;

import com.example.mountainwalker.entity.UserInfo;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserMapper {
    // 查询所有用户
    @Select("select * from user_info")
    List<UserInfo> getAllUsers();

    // 模糊查询（不查user_id）
    @Select("<script>" +
            "SELECT * FROM user_info " +
            "<where>" +
            "  <if test='keyword != null and keyword != \"\"'> " +
            "    (user_name LIKE CONCAT('%', #{keyword}, '%') " +
            "    OR contact_phone LIKE CONCAT('%', #{keyword}, '%') " +
            "    OR user_email LIKE CONCAT('%', #{keyword}, '%'))" +
            "  </if>" +
            "</where>" +
            "</script>")
    List<UserInfo> getUserByKeyword(@Param("keyword") String keyword);

    @Select("select * from user_info where user_id = #{id}")
    UserInfo getUserById(Integer id);

    @Delete("delete from user_info where user_id = #{id}")
    int deleteUserById(Integer id);

    @Insert("INSERT INTO user_info(user_id,user_name, user_avatar, signature, gender, contact_phone, " +
            "personal_introduction, user_email, date_of_birth, password, registration_time, modification_time) " +
            "VALUES(#{userId}, #{userName}, #{userAvatar}, #{signature}, #{gender}, #{contactPhone}, " +
            "#{personalIntroduction}, #{userEmail}, #{dateOfBirth}, #{password}, #{registrationTime}, #{modificationTime})")
    int addUser(UserInfo userInfo);

    @Update("UPDATE user_info SET user_name=#{userName}, user_avatar=#{userAvatar}, signature=#{signature}, " +
            "gender=#{gender}, contact_phone=#{contactPhone}, personal_introduction=#{personalIntroduction}, " +
            "user_email=#{userEmail}, date_of_birth=#{dateOfBirth}, password=#{password}, " +
            "modification_time=#{modificationTime} WHERE user_id=#{userId}")
    int updateUser(UserInfo userInfo);
}